package net.caixiaomi.info.adapter;

import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseMultiItemQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.model.OrderInfoItem;
import net.caixiaomi.info.util.Constants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单详情
 */
public class OrderInfoAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public OrderInfoAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(Constants.ITEM_VIEW_HEADER, R.layout.header_order_info_header);
        addItemType(Constants.ITEM_VIEW_ITEM, R.layout.item_order_info);

    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        switch (item.getItemType()) {
            case Constants.ITEM_VIEW_HEADER:
                bindHeader(helper, item);
                break;
            case Constants.ITEM_VIEW_ITEM:
                bindItem(helper, item);
                break;

        }
    }

    private void bindItem(BaseViewHolder helper, MultiItemEntity item) {
        try {
            OrderInfoItem model = (OrderInfoItem) item;

            StringBuilder stringBuilder = new StringBuilder();
            helper.itemView.setPadding((int) CommonApp.getInstance().getResources().getDimension(net.caixiaomi.info.R.dimen.padding_12), 0, (int) CommonApp.getInstance().getResources().getDimension(net.caixiaomi.info.R.dimen.padding_12), 0);
            helper.setVisible(R.id.line, (helper.getAdapterPosition() - getHeaderLayoutCount() - getFooterLayoutCount()) != (getData().size() - 2));

            String detailType = model.getDetailType();
            boolean normal = TextUtils.equals(detailType, "0");
            helper.setGone(R.id.num, normal);
            helper.setGone(R.id.way, normal);

            helper.setGone(R.id.flag, TextUtils.equals(model.getIsDan(), "1"));

            helper.setTextColor(R.id.name, ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text));

            if (normal) {//普通订单
                stringBuilder.setLength(0);
                stringBuilder.append(model.getChangci());
                stringBuilder.insert(2, "\n");
                helper.setTextColor(R.id.num, ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text));
                helper.setText(R.id.num, stringBuilder);

                stringBuilder.setLength(0);
                String[] vses = model.getMatch().split("VS");
                stringBuilder.append(vses[0].trim()).append("\n").append("VS").append("\n").append(vses[1].trim());
                helper.setText(R.id.name, stringBuilder);

            } else {//世界杯
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.weight = 1.5f;
                helper.getView(R.id.name).setLayoutParams(layoutParams);
                layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.weight = 1.5f;
                helper.getView(R.id.betting).setLayoutParams(layoutParams);
                layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.weight = 1.0f;
                helper.getView(R.id.result).setLayoutParams(layoutParams);

                helper.setText(R.id.name, model.getMatch());
            }

            stringBuilder.setLength(0);
            StringBuilder resultBuilder = new StringBuilder();
            StringBuilder playTypes = new StringBuilder();

            List<String> hightLight = new ArrayList<>();
            Map<Integer, String> resultHightLight = new HashMap<>();
            List<OrderInfoItem.CathecticResultsBean> cathecticResults = model.getCathecticResults();
            for (OrderInfoItem.CathecticResultsBean result : cathecticResults) {
                playTypes.append(result.getPlayType());
                List<OrderInfoItem.CathecticResultsBean.CathecticsBean> cathectics = result.getCathectics();
                for (OrderInfoItem.CathecticResultsBean.CathecticsBean cathectic : cathectics) {
                    stringBuilder.append(cathectic.getCathectic());
                    stringBuilder.append("\n");
                    if (TextUtils.equals("1", cathectic.getIsGuess())) {
                        hightLight.add(cathectic.getCathectic());
                        resultHightLight.put(resultBuilder.length(), result.getMatchResult());
                    }
                }
                resultBuilder.append(result.getMatchResult());
                resultBuilder.append("\n");
                playTypes.append("\n");
            }
            if (!TextUtils.isEmpty(stringBuilder)) {
                stringBuilder.deleteCharAt(stringBuilder.length() - 1);
            }
            if (!TextUtils.isEmpty(resultBuilder)) {
                resultBuilder.deleteCharAt(resultBuilder.length() - 1);
            }
            if (!TextUtils.isEmpty(playTypes)) {
                playTypes.deleteCharAt(playTypes.length() - 1);
            }

            SpannableString spannableString = new SpannableString(stringBuilder);
            for (String hight : hightLight) {
                int start = stringBuilder.indexOf(hight);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary)), start, start + hight.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            }

            SpannableString resultSpannableString = new SpannableString(resultBuilder);
            for (Map.Entry entry : resultHightLight.entrySet()) {
                int start = (int) entry.getKey();
                String hight = (String) entry.getValue();
                resultSpannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary)), start, start + hight.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            }

            helper.setTextColor(R.id.way, ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text));
            helper.setText(R.id.way, playTypes);

            helper.setTextColor(R.id.betting, ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text));
            helper.setText(R.id.betting, spannableString);

            helper.setTextColor(R.id.result, ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text));
            helper.setText(R.id.result, resultSpannableString);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void bindHeader(BaseViewHolder helper, MultiItemEntity item) {
        try {
            OrderInfoItem model = (OrderInfoItem) item;
            String detailType = model.getDetailType();
            boolean normal = TextUtils.equals(detailType, "0");
            helper.setGone(R.id.num, normal);
            helper.setGone(R.id.way, normal);
            helper.setText(R.id.name, TextUtils.equals(detailType, "1") ? R.string.C_GUESS_CHAMPION_TEAM : TextUtils.equals(detailType, "2") ? R.string.C_GUESS_CHAMPION_SECOND_TEAM : R.string.C_PLAY_GAME);
            if (!normal) {
                helper.setText(R.id.betting, R.string.C_RATE);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.weight = 1.5f;
                helper.getView(R.id.name).setLayoutParams(layoutParams);
                layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.weight = 1.5f;
                helper.getView(R.id.betting).setLayoutParams(layoutParams);
                layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.weight = 1.0f;
                helper.getView(R.id.result).setLayoutParams(layoutParams);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
